/*
 * Created on Dec 28, 2003
 * 
 * To change the template for this generated file go to Window - Preferences -
 * Java - Code Generation - Code and Comments
 */
package org.vectrics.recruiter.struts.admin;

import javax.servlet.jsp.JspException;

import org.vectrics.struts.Request;
import org.vectrics.struts.UserBufferManager;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.user.Person;
import org.vectrics.user.web.struts.ShowUserEditAction;
import org.vectrics.user.web.struts.UserProfileForm;

import com.hexacta.hrs.securitygroups.PermissionNameConverter;
import com.hexacta.hrs.web.struts.actions.security.PermissionChecker;

/**
 * @author mmoore
 */
public class ShowMyProfileAction extends ShowUserEditAction {

    private String failure = "failure";

    private String success = "success";

    private Boolean isEmployee;

    protected UserProfileForm createUserProfileForm() {
        return new AdminUserProfileForm();
    }

    @Override
    public String doExecute(VectricsActionData actionData) throws JspException {

        Boolean isEmployee = isEmployee(actionData);
        actionData.getRequest().setAttribute("isEmployee", isEmployee);

        String personId = actionData.getRequest().getParameter("personId");
        if (personId.equals("current")) {
            return super.doExecute(actionData);
        } else {
            boolean hasAccess = checkEmployeeOrCandidateAccess(personId, actionData);
            return hasAccess ? super.doExecute(actionData) : failure;
        }
    }

    private boolean checkEmployeeOrCandidateAccess(String personId, VectricsActionData actionData) {
        PermissionNameConverter permissionNameConverter = new PermissionNameConverter();
        PermissionChecker permissionChecker = new PermissionChecker();

        if (isEmployee) {
            // Si el usuario quiere ver detalles de un empleado, me fijo que
            // tenga permisos para hacerlo.
            String permission = permissionNameConverter.getViewEmployeesDetailsPermission().toString();
            Boolean hasViewEmployeesDetailsPermission = permissionChecker.hasPermission(permission, actionData
                    .getRequest().getSession());

            actionData.getRequest().getSession().setAttribute("accessError", true);
            return hasViewEmployeesDetailsPermission;
        } else {
            // Si el usuario quiere ver detalles de un candidato, me fijo
            // que
            // tenga permisos para hacerlo.
            String permission = permissionNameConverter.getViewCandidateDetailsPermission().toString();
            Boolean hasViewCandidateDetailsPermission = permissionChecker.hasPermission(permission, actionData
                    .getRequest().getSession());

            actionData.getRequest().getSession().setAttribute("accessError", true);
            return hasViewCandidateDetailsPermission;
        }
    }

    protected void onPersonFound(Request request, Person person) {
        UserBufferManager bufMgr = new UserBufferManager();
        bufMgr.addBufferedObject(request, person);
    }

}
